// news reducer

// 头部接口
//  async fetchNewsHeader() {
//   this.props.dispatch(async (dispatch) => {
//     const headerArr = await axios.get('/api/header')
//     dispatch({ type: 'init_news_header', payload: headerArr.data.data })
//   })
// }

// // tab切换接口
// async fetchNewsList(type = 'headline') {
//   this.props.dispatch(async (dispatch) => {
//     const newsData = await axios.get(`/api/news?type=${type}`)
//     console.log('new:', newsData.data.data)
//     dispatch({ type: 'init_news_data', payload: newsData.data.data[0].children })
//   })
// }
import axios from 'axios'
export const actionCreator = {
  initNews() {
    return async (dispatch) => {
      const headerArr = await axios.get('/api/header')
      dispatch({ type: 'init_news_header', payload: headerArr.data.data })
    }
  },
  initNewsData(type) {
    return async (dispatch) => {
      const newsData = await axios.get(`/api/news?type=${type}`)
      console.log('new:', newsData.data.data)
      dispatch({ type: 'init_news_data', payload: newsData.data.data[0].children })
    }
  },
}

const defaultState = {
  newsList: [],
  headerArr: [],
}
const news = (state = defaultState, action) => {
  const newState = JSON.parse(JSON.stringify(state))
  if (action.type === 'init_news_data') {
    newState.newsList = action.payload
  }
  if (action.type === 'init_news_header') {
    newState.headerArr = action.payload
  }
  return newState
}

export default news
